// pages/video/video.js
import {getVideoListByTag, getVideotag,getVideoAddressByVid,getResourceLike} from '../../service/video.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navFlog:0,
    VideotagList:[],
    VideoGroupList:[],
    cookie:JSON.parse(wx.getStorageSync('cookie')),
    curNavId:'',
    videoAddress:'',
    curVid:'',
    videoObj:[]
  },

  // 激活nav
  navClickHandeler(e){
    let tagid = e.currentTarget.dataset.tagid;
    this.setData({
      curNavId:tagid
    })
    this.getVideoListByTagData(tagid)

  },
  // 请求列表标签名
  async getVideotagData(){
    let {data} = await getVideotag({})
    let splicData = data.slice(0,14)
    this.setData({
      VideotagList:data,
      curNavId:splicData[0].id
    })
    this.getVideoListByTagData()
  },
  // 通过标签请求视频列表
  async getVideoListByTagData(){
    let {datas} = await getVideoListByTag({id:this.data.curNavId,cookie:this.data.cookie})
    let mapdatas = datas.map((item)=>{
      return item.data
    })
    this.setData({
      VideoGroupList:mapdatas || []
    })
    
  },
  // 播放视频
  async playVideo(e){
    let vid = e.currentTarget.dataset.vid;
    let {urls} = await  getVideoAddressByVid({id:vid});
    let videoAddress = urls[0].url;
    let VideoContext = wx.createVideoContext(vid)
    // 判断该视频之前是否存在播放记录
    let isPlayBefore = this.data.videoObj.find((item)=>item.vid == vid)
    console.log(isPlayBefore);
    this.setData({
      videoAddress,
      curVid:vid
    })
    if(isPlayBefore){
      // 如果存在则让该视频跳转到相应的进度条
      console.log(VideoContext);
      VideoContext.seek(isPlayBefore.curPlayTime)
    }
    
  },
  // 滚动底部 请求新数据
  async onScrolltolower(e){
    wx.showToast({
      title: '已经滑到底啦',
      icon:'none'
    })
    let {datas} = await getVideoListByTag({id:this.data.curNavId,cookie:this.data.cookie})
    let filterdatas = datas.filter((item)=>{
      return item.data
    })
    console.log(filterdatas);
  },
  // 视频点赞
  async videoLike(e){
    let vid  = e.currentTarget.dataset.vid;
    let cookie = JSON.parse(wx.getStorageSync('cookie'));
    let ispraised = e.currentTarget.dataset.praised
    if(!cookie){
      wx.showToast({
        title: '先登录才能够评论哦',
        icon:'error'
      })
    }else{
      if(ispraised){
        // 如果已经点过赞，则取消点赞
        console.log(e);
        let {code} = await getResourceLike({t:'quxiao',type:5,id:vid,cookie});
        if(code==200){
          this.getVideoListByTagData()
        }
      }else{
        // 如果没有点过赞，则发送点赞请求
        let {code} = await getResourceLike({t:1,type:5,id:vid,cookie});
        if(code==200){
          this.getVideoListByTagData()
        }
      }

    }
  },

  // 视频评论
  videoComment(){},
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.getVideotagData()
    // await this.getVideoListByTagData()
  },
// 监听视频播放回调
onTimeUpdate(e){
  let vid = e.currentTarget.id;
  let curPlayTime = e.detail.currentTime; //获取当前视频播放的时间
  let videoObj = this.data.videoObj;
  let videoItem = videoObj.find((item)=>item.vid == this.data.curVid) 
  // 判断数组中是否已经存在vid,如果存在则只修改时间
  if(videoItem){
    videoItem.curPlayTime = curPlayTime
  }else{
    videoObj.push({vid,curPlayTime})
  }
  this.setData({
    videoObj
  })
},

  // 视频暂停回调
  onPause(e){
    console.log(e);
  },
  // 跳转到搜索音乐页面
  toSearch(){
    wx.navigateTo({
      url: '/pages/subpackages/pages/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})